﻿using AutoMapper;
using B.S.Data.MES.API.Dto.DTOProcess;
using B.S.Domain.Process.ProcessmaintenanceModel;
using B.S.Domain.Process.ProcessrouteName;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Data.MES.API.Applications.ProcessrouteApplication
{
    public class ShowProcessrouteCommandHorter:IRequestHandler<ShowProcessrouteCommand,APIPageing<List<DtoProcessroute>>>
    {
             IMapper mediator;
        IBaseRepository<Processroute> processmaintenanceReq;

        public ShowProcessrouteCommandHorter(IMapper mediator,IBaseRepository<Processroute> processmaintenanceReq)
        {
            this.mediator = mediator;
            this.processmaintenanceReq = processmaintenanceReq;
        }

        public async Task<APIPageing<List<DtoProcessroute>>> Handle(ShowProcessrouteCommand request, CancellationToken cancellationToken)
        {
            var res = new APIPageing<List<DtoProcessroute>>();

            // 使用IQueryable进行查询，不立即执行
            var query = processmaintenanceReq.GetAll().Where(o=>o.IsDel==false);

            // 添加过滤条件（如果需要）
            if (!string.IsNullOrEmpty(request.ProcessrouteName))
            {
                query = query.Where(x => x.ProcessrouteName.Contains(request.ProcessrouteName));
            }
            //if (!string.IsNullOrEmpty(request.Image))
            //{
            //    query = query.Where(x => x.Image.Contains(request.Image));
            //}
            //if (!string.IsNullOrEmpty(request.ProcessrouteType))
            //{
            //    query = query.Where(x => x.ProcessrouteType.Contains(request.ProcessrouteType));
            //}        
            //if (request.ProcessrouteDescription != null)
            //{
            //    query = query.Where(x => x.ProcessrouteDescription == request.ProcessrouteDescription);
            //}
            //if (!string.IsNullOrEmpty(request.ProcessrouteCreatorName))
            //{
            //    query = query.Where(x => x.ProcessrouteCreatorName.Contains(request.ProcessrouteCreatorName));
            //}
            if (request.ProcessrouteState != null)
            {
                query = query.Where(x => x.ProcessrouteState==request.ProcessrouteState);
            }

            //if (request.ProcessrouteCreatorTimeKK != null || request.ProcessrouteCreatorTimeJJ != null)
            //{
            //    query = query.Where(x => x.ProcessrouteCreatorTime >= request.ProcessrouteCreatorTimeKK && x.ProcessrouteCreatorTime <= request.ProcessrouteCreatorTimeJJ);
            //}
            // 异步获取总记录数
            res.TotalCount = await query.CountAsync(cancellationToken);
            res.Total = (int)Math.Ceiling(res.TotalCount * 1.0 / request.PageSize);

            // 异步执行分页查询
            var pagelist = await query.OrderByDescending(o => o.Id)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync(cancellationToken);

            // 正确使用AutoMapper将实体列表映射到DTO列表
            var dtoList = mediator.Map<List<DtoProcessroute>>(pagelist);
            res.Data = dtoList;

            return res;
        }
    }
}
